<template>
    <div>
        <router-view></router-view>
        <div class="global-progress" v-show="loading">
            <mu-circular-progress :size="60" :strokeWidth="5"/>
            <div class="mu-overlay" style=" position: fixed; z-index: 20141223;"></div>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                loading: false
            }
        },
        created(){
            var self = this;
            this.$http.interceptors.request.use(
                config => {
                    var xtoken = self.$root.userInfo.token
                    if (xtoken != null) {
                        config.headers.common['authorization'] = 'bearer' + xtoken;
                    }
                    self.$loading.start();
                    return config;
                }, function (error) {
                    return Promise.reject(error);
                }
            )
            this.$http.interceptors.response.use(function (response) {
                self.$loading.done();
                // token 已过期，重定向到登录页面
                if (response.status_code == 401) {
                    self.$bus.emit('USER_LOGOUT');
                }
                return response
            }, function (error) {
                self.$loading.done();
                if (typeof error.response === 'undefined' ||
                    typeof error.response.data === 'undefined') {
                    self.$toast.error('网络异常');
                    return Promise.reject(error);
                }
                var response = error.response.data;
                if (response.status_code == 401) {
                    self.$toast.error(response.message);
                    self.$bus.emit('USER_LOGOUT');
                } else if (response.status_code == 400) {
                    self.$toast.error(response.message);
                } else {
                    self.$toast.error('网络异常');
                    return Promise.reject(error);
                }
            })

            this.$bus.on('USER_LOGOUT', function (toPath) {
                localStorage.removeItem('userInfo');//清空用户数据
                if (toPath != '/login') {//跳转登录页面
                    self.$router.replace('/login');
                }
            })
            this.$bus.on('USER_LOGIN', function (userInfo) {
                self.$root.userInfo = userInfo;
                localStorage.userInfo = JSON.stringify(userInfo);
                localStorage.account =  self.$root.userInfo.account;
                self.$router.replace('/admin/dashboard');
            });
            this.checkAuth(this.$root.userInfo, this.$router.path);
        },
        watch: {
            '$route'(to, from) {
                this.checkAuth(this.$root.userInfo, to.path);
            },
        },
        methods: {
            checkAuth(userInfo, path){
                if (!userInfo.token || userInfo.expired_at < (new Date()).valueOf()) {
                    this.$bus.emit('USER_LOGOUT', path);//退出登录
                } else {
                    if (this.$route.path == '/login') {
                        this.$bus.emit('USER_LOGIN', userInfo);//退出登录
                    }
                }
            },

        }
    }
</script>